package org.sixtythousand.joker.servlet.session;

import org.sixtythousand.joker.util.AutoLock;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Enumeration;

/**
 * @author zhoulk
 * @date 2023/10/13 17:18
 * @desc
 */
public class Session implements HttpSession {

    public enum State
    {
        VALID, INVALID, INVALIDATING, CHANGING
    }

    protected AutoLock lock = new AutoLock();

    protected State state;

    protected boolean resident = false;
    protected boolean newSession;

    protected   SessionData sessionData; // the actual data associated with


    protected boolean access(long time)
    {
        try (AutoLock l = lock.lock())
        {
            if (!isValid() || !isResident())
                return false;
            newSession = false;
            long lastAccessed = sessionData.getAccessed();
            sessionData.setAccessed(time);
            sessionData.setLastAccessed(lastAccessed);
            sessionData.calcAndSetExpiry(time);
            if (isExpiredAt(time))
            {
                invalidate();
                return false;
            }
            return true;
        }
    }

    public long getCookieSetTime()
    {
        try (AutoLock l = lock.lock())
        {
            return sessionData.getCookieSet();
        }
    }

    protected boolean isExpiredAt(long time)
    {
        try (AutoLock l = lock.lock())
        {
            return sessionData.isExpiredAt(time);
        }
    }

    public boolean isValid()
    {
        try (AutoLock l = lock.lock())
        {
            return state == State.VALID;
        }
    }

    public boolean isResident()
    {
        return resident;
    }

    public void cookieSet() {
        try (AutoLock l = lock.lock())
        {
            sessionData.setCookieSet(sessionData.getAccessed());
        }
    }


    @Override
    public long getCreationTime() {
        return 0;
    }

    @Override
    public String getId() {
        return null;
    }

    @Override
    public long getLastAccessedTime() {
        return 0;
    }

    @Override
    public ServletContext getServletContext() {
        return null;
    }

    @Override
    public void setMaxInactiveInterval(int interval) {

    }

    @Override
    public int getMaxInactiveInterval() {
        return 0;
    }

    @Override
    public HttpSessionContext getSessionContext() {
        return null;
    }

    @Override
    public Object getAttribute(String name) {
        return null;
    }

    @Override
    public Object getValue(String name) {
        return null;
    }

    @Override
    public Enumeration<String> getAttributeNames() {
        return null;
    }

    @Override
    public String[] getValueNames() {
        return new String[0];
    }

    @Override
    public void setAttribute(String name, Object value) {

    }

    @Override
    public void putValue(String name, Object value) {

    }

    @Override
    public void removeAttribute(String name) {

    }

    @Override
    public void removeValue(String name) {

    }

    @Override
    public void invalidate() {

    }

    @Override
    public boolean isNew() {
        return false;
    }
}
